What is object-visit?
The object-visit npm package is designed to simplify the process of visiting and invoking methods on an object. It allows you to call a specified method on each property of an object, which can be particularly useful for tasks like data transformation, validation, or applying a series of operations to an object's properties.
What are object-visit's main functionalities?
Invoke a method on each property of an object
This feature allows you to invoke a specified method on each property of an object. In this example, the 'greet' method is called on each property of the object, transforming the 'greet' property to its return value.
const visit = require('object-visit');
const obj = {
name: 'John',
age: 30,
greet: function() { return `Hello, my name is ${this.name}`; }
};
visit(obj, 'greet');
// Output: { name: 'John', age: 30, greet: 'Hello, my name is John' }
Invoke a method with arguments on each property of an object
This feature allows you to invoke a specified method with arguments on each property of an object. In this example, the 'greet' method is called with the argument 'Hi' on each property of the object, transforming the 'greet' property to its return value.
const visit = require('object-visit');
const obj = {
name: 'John',
age: 30,
greet: function(greeting) { return `${greeting}, my name is ${this.name}`; }
};
visit(obj, 'greet', 'Hi');
// Output: { name: 'John', age: 30, greet: 'Hi, my name is John' }
Other packages similar to object-visit
lodash
Lodash is a modern JavaScript utility library delivering modularity, performance, and extras. It provides a wide range of utility functions for common programming tasks, including object manipulation. Compared to object-visit, Lodash offers a broader set of functionalities but may be more complex to use for simple method invocation tasks.
underscore
Underscore is a JavaScript library that provides a whole mess of useful functional programming helpers without extending any built-in objects. It includes utilities for working with objects, arrays, and functions. While it offers similar object manipulation capabilities, it is not as focused on method invocation as object-visit.
ramda
Ramda is a practical functional library for JavaScript programmers. It makes it easy to create functional pipelines and work with immutable data structures. Ramda provides utilities for object manipulation, but its primary focus is on functional programming paradigms, making it different in scope compared to object-visit.
object-visit
Call a specified method on each value in the given object.
Install
Install with npm
$ npm i object-visit --save
Usage
var visit = require('object-visit');
var ctx = {
data: {},
set: function (key, value) {
if (typeof key === 'object') {
visit(ctx, 'set', key);
} else {
ctx.data[key] = value;
}
}
};
ctx.set('a', 'a');
ctx.set('b', 'b');
ctx.set('c', 'c');
ctx.set({d: {e: 'f'}});
console.log(ctx.data);
Related projects
Running tests
Install dev dependencies:
$ npm i -d && npm test
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Author
Jon Schlinkert
License
Copyright © 2015 Jon Schlinkert
Released under the MIT license.
This file was generated by verb-cli on November 09, 2015.